import { User, UserProfile } from '@/types/data'
import { UserAction } from '@/types/store'

type ProfileState = {
  user: User
  userProfile: UserProfile
}
const InitialState = {
  user: {},
  userProfile: {},
} as ProfileState

export const profile = (
  state = InitialState,
  action: UserAction
): ProfileState => {
  if (action.type === 'profile/getUser') {
    return {
      ...state,
      user: action.payload,
    }
  }
  if (action.type === 'profile/getUserProfile') {
    return {
      ...state,
      userProfile: action.payload,
    }
  }
  if (action.type === 'profile/update') {
    return {
      ...state,
      userProfile: {
        ...state.userProfile,
        ...action.payload,
      },
    }
  }
  if (action.type === 'profile/photo') {
    return {
      ...state,
      userProfile: {
        ...state.userProfile,
        photo: action.payload,
      },
    }
  }

  return state
}
